# 需要提前安装用于矩阵运算的numpy库
import numpy as np
# 同阶平均随机一致性检验指标
RI_dict = {1: 0, 2: 0, 3: 0.58, 4: 0.90, 5: 1.12, 6: 1.24, 7: 1.32, 8: 1.41, 9: 1.45}

# 矩阵
C = [[1, 3, 3, 5],
     [1 / 3, 1, 1, 2],
     [1 / 3, 1, 1, 2],
     [1 / 5, 1 / 2, 1 / 2, 1]]

B1 = [[1, 1 / 3, 1 / 5],
      [3, 1, 1 / 2],
      [5, 2, 1]]

B2 = [[1, 1 / 2, 2],
      [2, 1, 5],
      [1 / 2, 1 / 5, 1]]

B3 = [[1, 1 / 3, 1 / 2],
      [3, 1, 2],
      [2, 1 / 2, 1]]

B4 = [[1, 1 / 2, 1 / 2],
      [2, 1, 1],
      [2, 1, 1]]


def ahp(juzhen):
    A = np.array(juzhen)
    a_sum0 = A.sum(axis=0)
    B = A / a_sum0
    b_sum = B.sum(axis=1)
    W = b_sum.sum()
    w_arr = []
    for w in b_sum:
        w_arr.append(w / W)
    print('W: %s' % w_arr)
    AW = []
    for a in A:
        aa = a * w_arr
        AW.append(aa.sum())
    print('AW: %s' % AW)
    result = np.array(AW) / np.array(w_arr)
    print('AW/W: %s' % result)
    row = result.shape[0]
    Max = result.sum() / row
    print('λMax = {}'.format(round(Max, 7)))
    CI = (Max - row) / (row - 1)
    print('CI = {}'.format(round(CI, 10)))
    CR = CI / RI_dict[row]
    print('CR = {}'.format(round(CR, 10)), end='')
    if CR < 0.1:
        print(' < 0.1')
        print('通过一致性检验！')
    else:
        print(' > 0.1')
        print("一致性检验未通过")
    return W


ahp(C)  # 计算和检验矩阵C
for i in (B1, B2, B3, B4):  # 计算和检验其他矩阵
    ahp(i)
